from io import BytesIO
import json

from fastapi import APIRouter
from minio.error import S3Error
from seaman.core import JsonUtil


from common.method import *
from common.model import InterpolationData
from common.params import ReqSoftParams, SoftParams, StabilityParams
from service.DataPreService import read_interpolation_data

router = APIRouter()

class UniForceResult:
    # 深度
    depth: float
    inclination: float
    azimuth: float
    rh0: float
    drilling_fluid_density: float
    xb: float
    N: float
    is_Not_Safe: str


@router.post('/v3/stability/data')
async def well_stability_data(params: ReqSoftParams):
    # 计算 MD5 哈希值
    md5_hash = params.calc_md5()
    # 获取井号
    wellId: str = params.wellId
    # 获取间距
    space: float = params.space
    # 获取是否有造斜点
    is_slanting_point = params.is_slanting_point
    # 数据项
    data_object_name = wellId + '/' + md5_hash + '-stability.xlsx'
    try:
        client.stat_object('well', data_object_name)
        print('存在对应井壁稳定性数据')
        file_data = client.get_object("well", data_object_name)
        df = pd.read_excel(BytesIO(file_data.read()))
        data_list = []
        for index, row in df.iterrows():
            data_list.append(row)
        return data_list
    except S3Error:
        print('不存在对应井壁稳定性数据')
        # 获取插值数据
    interpolation_data_list: List[InterpolationData] = read_interpolation_data(wellId, space, is_slanting_point)
    # print(JsonUtil.obj2str(interpolation_data_list))
    stability: List[StabilityParams] = params.stability
    stability_result: List[UniForceResult] = []

    is_Not_Safe = "安全"
    for depth_list in interpolation_data_list:
        depth = depth_list.depth

        stability_inclination: float = depth_list.inclination
        stability_azimuth: float = depth_list.azimuth
        result = UniForceResult()
        result.depth = depth
        result.inclination = stability_inclination
        result.azimuth = stability_azimuth
        for c_item in stability:
            start_depth: float = c_item.start_depth
            end_depth: float = c_item.end_depth
            cohesion: float = c_item.cohesion
            internal_friction_angle: float = c_item.internal_friction_angle
            formation_stress_factor: float = c_item.formation_stress_factor
            poisson_ratio: float = c_item.poisson_ratio
            max_horizontal_ground_stress_gradient: float = c_item.max_horizontal_ground_stress_gradient
            min_horizontal_ground_stress_gradient: float = c_item.min_horizontal_ground_stress_gradient
            vertical_ground_stress_gradient: float = c_item.vertical_ground_stress_gradient
            original_liquid_density: float = c_item.original_liquid_density
            borehole_radius: float = c_item.borehole_radius
            void_ratio: float = c_item.void_ratio
            osmotic_coefficient: float = c_item.osmotic_coefficient
            yang_modulus_elasticity: float = c_item.yang_modulus_elasticity
            drilling_fluid_density: float = c_item.drilling_fluid_density
            # 先判断接收的所有参数是否全部为0
            # ***********************************
            if all([start_depth == 0,
                    end_depth == 0,
                    cohesion == 0,
                    internal_friction_angle == 0,
                    formation_stress_factor == 0,
                    poisson_ratio == 0,
                    max_horizontal_ground_stress_gradient == 0,
                    min_horizontal_ground_stress_gradient == 0,
                    vertical_ground_stress_gradient == 0,
                    original_liquid_density == 0,
                    borehole_radius == 0,
                    void_ratio == 0,
                    osmotic_coefficient == 0,
                    yang_modulus_elasticity == 0,
                    drilling_fluid_density == 0,
                    stability_inclination == 0,
                    stability_azimuth == 0]):

                continue  # 如果所有参数都为0，跳过当前循环，不进行计算
            # ***********************************

            if (depth >= start_depth) and (depth <= end_depth):
                max_horizontal_ground_stress: float = (max_horizontal_ground_stress_gradient * depth) / 1000
                min_horizontal_ground_stress: float = (min_horizontal_ground_stress_gradient * depth) / 1000
                vertical_ground_stress: float = (vertical_ground_stress_gradient * depth) / 1000
                in_situ_pore_pressure: float = (original_liquid_density * 9.8 * depth) / 1000

                # 求解sigma_xx
                sigma_xx: float = (max_horizontal_ground_stress * math.pow(math.cos(math.radians(stability_azimuth)), 2)
                            + min_horizontal_ground_stress * math.pow(math.sin(math.radians(stability_azimuth)), 2)) * math.pow(
                    math.cos(math.radians(stability_inclination)), 2) + vertical_ground_stress * math.pow(
                    math.sin(math.radians(stability_inclination)), 2)

                # print("depth为{}，sigma_xx为{}".format(depth,sigma_xx))
                # 求解sigma_yy
                sigma_yy: float = max_horizontal_ground_stress * math.pow(math.sin(math.radians(stability_azimuth)), 2) + \
                           min_horizontal_ground_stress * math.pow(math.cos(math.radians(stability_azimuth)), 2)
                # print("depth为{}，sigma_yy为{}".format(depth, sigma_yy))
                # 求解sigma_zz
                sigma_zz: float = (max_horizontal_ground_stress * math.pow(math.cos(math.radians(stability_azimuth)), 2)
                            + min_horizontal_ground_stress * math.pow(math.sin(math.radians(stability_azimuth)), 2)) * math.pow(
                    math.sin(math.radians(stability_inclination)), 2) + vertical_ground_stress * math.pow(
                    math.cos(math.radians(stability_inclination)), 2)
                # print("depth为{}，sigma_zz为{}".format(depth, sigma_zz))

                # 求解tau_xy
                tau_xy: float = (min_horizontal_ground_stress - max_horizontal_ground_stress) * 0.5 * math.sin(
                    math.radians(2 * stability_azimuth)) * math.cos(math.radians(stability_inclination))
                # print("depth为{}，tau_xy为{}".format(depth, tau_xy))

                # 求解tau_yz
                tau_yz: float = (min_horizontal_ground_stress - max_horizontal_ground_stress) * 0.5 * math.sin(
                    math.radians(2 * stability_azimuth)) * math.sin(math.radians(stability_inclination))
                # print("depth为{}，tau_yz为{}".format(depth, tau_yz))

                # 求解tau_xz
                tau_xz: float = (max_horizontal_ground_stress * math.pow(math.cos(math.radians(stability_azimuth)), 2)
                          + min_horizontal_ground_stress * math.pow(math.sin(math.radians(stability_azimuth)), 2)
                          - vertical_ground_stress) * 0.5 * math.sin(math.radians(2 * stability_inclination))
                # print("depth为{}，tau_xz为{}".format(depth, tau_xz))

                # 井周角计算-------从0---180迭代计算 z取最小时 拿到theta值
                max_theta: float = 180
                step_size: float = 0.1  # 调整步长为 0.1 度
                min_z = float('inf')  # 用于跟踪最小的 z 值
                optimal_theta: float = 0  # 用于跟踪对应最小 z 值的 theta
                theta: float = 0
                while theta <= max_theta:
                    z: float = 2 * (sigma_xx - sigma_yy) * math.cos(2 * math.radians(theta)) + 4 * tau_xy * math.sin(
                        2 * math.radians(theta))
                    if z < min_z:
                        min_z = z
                        optimal_theta = theta
                    theta += step_size
                # 失稳准则： D.P准则
                factor_a: float = math.pow(math.sin(math.radians(internal_friction_angle)), 2)
                R: float = (math.sqrt(3) * math.sin(math.radians(internal_friction_angle))) / (3 * math.sqrt(3 + factor_a))
                # print("depth为{}，R为{}".format(depth, R))

                K: float = (math.sqrt(3) * cohesion * math.cos(math.radians(internal_friction_angle))) / math.sqrt(3 + factor_a)
                # print("depth为{}，K为{}".format(depth, K))

                #  计算Pm  临界密度
                p_m: float = 1.0
                # 设置迭代的要求
                step_size: float = 0.1  # 迭代步长为 0.1
                # 定义停止条件
                tolerance = 1e-6  # 容差值
                max_iterations = 1000  # 最大迭代次数
                iteration = 0
                theta: float = optimal_theta
                # theta = 86.3
                # 求解三个带有p_m的公式
                while True:

                    # *******************************
                    # 计算sigma_r
                    sigma_r: float = p_m - osmotic_coefficient * void_ratio * (p_m - in_situ_pore_pressure)
                    # 计算sigma_theta
                    factor_a: float = (formation_stress_factor * (1 - 2 * poisson_ratio)) / (1 - poisson_ratio)
                    sigma_theta: float = sigma_xx + sigma_yy - 2 * (sigma_xx - sigma_yy) * math.cos(math.radians(2 * theta)) - \
                                  4 * tau_xy * math.sin(math.radians(2 * theta)) - p_m + \
                                  osmotic_coefficient * (p_m - in_situ_pore_pressure) * (factor_a - void_ratio)
                    # 计算sigma_z
                    sigma_z: float = sigma_zz - (2 * poisson_ratio * (sigma_xx - sigma_yy) * math.cos(math.radians(2 * theta)) -
                                          4 * poisson_ratio * tau_xy * math.sin(math.radians(2 * theta))) + \
                              osmotic_coefficient * (p_m - in_situ_pore_pressure) * (factor_a * 0.5 - void_ratio)
                    # *******************************
                    # 计算tau_theta_z
                    tau_theta_z: float = 2 * tau_yz * math.cos(math.radians(theta)) - 2 * tau_xz * math.sin(math.radians(theta))
                    # 计算tau_rz, tau_r_theta
                    tau_rz: float = 0
                    tau_r_theta: float = 0
                    # 计算sigma_one
                    sigma_one: float = sigma_r - formation_stress_factor * in_situ_pore_pressure
                    # 计算sigma_two
                    factor_b: float = (sigma_theta + sigma_z) / 2
                    factor_c: float = math.pow((sigma_theta - sigma_z), 2) + 4 * math.pow(tau_theta_z, 2)
                    factor_d: float = formation_stress_factor * in_situ_pore_pressure
                    sigma_two: float = (factor_b + math.sqrt(factor_c) / 2) - factor_d
                    # 计算sigma_three
                    sigma_three: float = (factor_b - math.sqrt(factor_c) / 2) - factor_d
                    # 计算J_two
                    factor_e: float = math.pow((sigma_one - sigma_two), 2)
                    factor_f: float = math.pow((sigma_two - sigma_three), 2)
                    factor_g: float = math.pow((sigma_three - sigma_one), 2)
                    J_two: float = (factor_e + factor_f + factor_g) / 6
                    # 计算I_one
                    I_one: float = sigma_one + sigma_two + sigma_three
                    y_result: float = math.sqrt(J_two) - R * I_one - K
                    # 更新 p_m 的值
                    p_m += step_size
                    # 检查停止条件
                    if y_result < tolerance:
                        p_m = p_m
                        break
                    iteration += 1
                # print("depth为{}，sigma_r为{}".format(depth, sigma_r))
                # print("depth为{}，sigma_theta为{}".format(depth, sigma_theta))
                # print("depth为{}，sigma_z为{}".format(depth, sigma_z))
                # print("depth为{}，tau_theta_z为{}".format(depth, tau_theta_z))
                # print("depth为{}，sigma_one为{}".format(depth, sigma_one))
                # print("depth为{}，sigma_two为{}".format(depth, sigma_two))
                # print("depth为{}，sigma_three为{}".format(depth, sigma_three))
                # print("depth为{}，J_two为{}".format(depth, J_two))
                # print("depth为{}，I_one为{}".format(depth, I_one))
                # print("depth为{}，y_result为{}".format(depth, y_result))
                # print("depth为{}，p_m为{}".format(depth, p_m))
                # 临界钻井液密度
                rh0 = (p_m / depth) * 100
                result.rh0 = rh0
                # print("depth为{}，rh0为{}".format(depth, rh0))
                xb: float = drilling_fluid_density / rh0
                result.drilling_fluid_density = drilling_fluid_density
                result.xb = xb
                # print("depth为{}，xb为{}".format(depth, xb))
                N = ((3 * 2 * borehole_radius) / yang_modulus_elasticity) * (
                        (max_horizontal_ground_stress + min_horizontal_ground_stress) + 2 * (
                        max_horizontal_ground_stress - min_horizontal_ground_stress))
                # print("depth为{}，N为{}".format(depth, N))
                result.N = N

                if xb < 1:
                    is_Not_Safe = "危险"
                    result.is_Not_Safe = is_Not_Safe

                elif (xb >= 1) and (xb < 1.2):
                    is_Not_Safe = "基本安全"
                    result.is_Not_Safe = is_Not_Safe

                elif xb > 1.2:
                    is_Not_Safe = "安全"
                    result.is_Not_Safe = is_Not_Safe

                stability_result.append(result)
    # print(JsonUtil.obj2str(stability_result))
    stability_result_json = json.loads(JsonUtil.obj2str(stability_result))

    df = pd.DataFrame(stability_result_json)
    # 生成并上传Excel数据
    excel_data = df
    # 将数据写入Excel文件
    excel_file = io.BytesIO()
    excel_data.to_excel(excel_file, index=False)
    excel_file.seek(0)
    # putObject('well', data_object_name, excel_file)
    return stability_result_json

@router.post('/v3/stability/conclusion')
async def well_stability_conclusion(params: ReqSoftParams):
    # 计算 MD5 哈希值
    md5_hash = params.calc_md5()
    # 获取井号
    wellId: str = params.wellId
    # 获取间距
    space: float = params.space
    # 获取是否有造斜点
    is_slanting_point = params.is_slanting_point
    # 数据项
    data_object_name = wellId + '/' + md5_hash + '-stability.xlsx'
    try:
        client.stat_object('well', data_object_name)
        print('存在对应井壁稳定性数据')
        file_data = client.get_object("well", data_object_name)
        df = pd.read_excel(BytesIO(file_data.read()))
        data_list = []
        for index, row in df.iterrows():
            data_list.append(row)
        # return data_list
    except S3Error:
        print('不存在对应井壁稳定性数据')
        # 获取插值数据
    interpolation_data_list: List[InterpolationData] = read_interpolation_data(wellId, space, is_slanting_point)
    # print(JsonUtil.obj2str(interpolation_data_list))
    stability: List[StabilityParams] = params.stability
    stability_result: List[UniForceResult] = []

    is_Not_Safe = "安全"
    for depth_list in interpolation_data_list:
        depth = depth_list.depth

        stability_inclination: float = depth_list.inclination
        stability_azimuth: float = depth_list.azimuth
        result = UniForceResult()
        result.depth = depth
        result.inclination = stability_inclination
        result.azimuth = stability_azimuth
        for c_item in stability:
            start_depth: float = c_item.start_depth
            end_depth: float = c_item.end_depth
            cohesion: float = c_item.cohesion
            internal_friction_angle: float = c_item.internal_friction_angle
            formation_stress_factor: float = c_item.formation_stress_factor
            poisson_ratio: float = c_item.poisson_ratio
            max_horizontal_ground_stress_gradient: float = c_item.max_horizontal_ground_stress_gradient
            min_horizontal_ground_stress_gradient: float = c_item.min_horizontal_ground_stress_gradient
            vertical_ground_stress_gradient: float = c_item.vertical_ground_stress_gradient
            original_liquid_density: float = c_item.original_liquid_density
            borehole_radius: float = c_item.borehole_radius
            void_ratio: float = c_item.void_ratio
            osmotic_coefficient: float = c_item.osmotic_coefficient
            yang_modulus_elasticity: float = c_item.yang_modulus_elasticity
            drilling_fluid_density: float = c_item.drilling_fluid_density
            # 先判断接收的所有参数是否全部为0
            # ***********************************
            if all([start_depth == 0,
                    end_depth == 0,
                    cohesion == 0,
                    internal_friction_angle == 0,
                    formation_stress_factor == 0,
                    poisson_ratio == 0,
                    max_horizontal_ground_stress_gradient == 0,
                    min_horizontal_ground_stress_gradient == 0,
                    vertical_ground_stress_gradient == 0,
                    original_liquid_density == 0,
                    borehole_radius == 0,
                    void_ratio == 0,
                    osmotic_coefficient == 0,
                    yang_modulus_elasticity == 0,
                    drilling_fluid_density == 0,
                    stability_inclination == 0,
                    stability_azimuth == 0]):

                continue  # 如果所有参数都为0，跳过当前循环，不进行计算
            # ***********************************

            if (depth >= start_depth) and (depth <= end_depth):
                max_horizontal_ground_stress: float = (max_horizontal_ground_stress_gradient * depth) / 1000
                min_horizontal_ground_stress: float = (min_horizontal_ground_stress_gradient * depth) / 1000
                vertical_ground_stress: float = (vertical_ground_stress_gradient * depth) / 1000
                in_situ_pore_pressure: float = (original_liquid_density * 9.8 * depth) / 1000

                # 求解sigma_xx
                sigma_xx: float = (max_horizontal_ground_stress * math.pow(math.cos(math.radians(stability_azimuth)), 2)
                            + min_horizontal_ground_stress * math.pow(math.sin(math.radians(stability_azimuth)), 2)) * math.pow(
                    math.cos(math.radians(stability_inclination)), 2) + vertical_ground_stress * math.pow(
                    math.sin(math.radians(stability_inclination)), 2)

                # print("depth为{}，sigma_xx为{}".format(depth,sigma_xx))
                # 求解sigma_yy
                sigma_yy: float = max_horizontal_ground_stress * math.pow(math.sin(math.radians(stability_azimuth)), 2) + \
                           min_horizontal_ground_stress * math.pow(math.cos(math.radians(stability_azimuth)), 2)
                # print("depth为{}，sigma_yy为{}".format(depth, sigma_yy))
                # 求解sigma_zz
                sigma_zz: float = (max_horizontal_ground_stress * math.pow(math.cos(math.radians(stability_azimuth)), 2)
                            + min_horizontal_ground_stress * math.pow(math.sin(math.radians(stability_azimuth)), 2)) * math.pow(
                    math.sin(math.radians(stability_inclination)), 2) + vertical_ground_stress * math.pow(
                    math.cos(math.radians(stability_inclination)), 2)
                # print("depth为{}，sigma_zz为{}".format(depth, sigma_zz))

                # 求解tau_xy
                tau_xy: float = (min_horizontal_ground_stress - max_horizontal_ground_stress) * 0.5 * math.sin(
                    math.radians(2 * stability_azimuth)) * math.cos(math.radians(stability_inclination))
                # print("depth为{}，tau_xy为{}".format(depth, tau_xy))

                # 求解tau_yz
                tau_yz: float = (min_horizontal_ground_stress - max_horizontal_ground_stress) * 0.5 * math.sin(
                    math.radians(2 * stability_azimuth)) * math.sin(math.radians(stability_inclination))
                # print("depth为{}，tau_yz为{}".format(depth, tau_yz))

                # 求解tau_xz
                tau_xz: float = (max_horizontal_ground_stress * math.pow(math.cos(math.radians(stability_azimuth)), 2)
                          + min_horizontal_ground_stress * math.pow(math.sin(math.radians(stability_azimuth)), 2)
                          - vertical_ground_stress) * 0.5 * math.sin(math.radians(2 * stability_inclination))
                # print("depth为{}，tau_xz为{}".format(depth, tau_xz))

                # 井周角计算-------从0---180迭代计算 z取最小时 拿到theta值
                max_theta: float = 180
                step_size: float = 0.1  # 调整步长为 0.1 度
                min_z = float('inf')  # 用于跟踪最小的 z 值
                optimal_theta: float = 0  # 用于跟踪对应最小 z 值的 theta
                theta: float = 0
                while theta <= max_theta:
                    z: float = 2 * (sigma_xx - sigma_yy) * math.cos(2 * math.radians(theta)) + 4 * tau_xy * math.sin(
                        2 * math.radians(theta))
                    if z < min_z:
                        min_z = z
                        optimal_theta = theta
                    theta += step_size
                # 失稳准则： D.P准则
                factor_a: float = math.pow(math.sin(math.radians(internal_friction_angle)), 2)
                R: float = (math.sqrt(3) * math.sin(math.radians(internal_friction_angle))) / (3 * math.sqrt(3 + factor_a))
                # print("depth为{}，R为{}".format(depth, R))

                K: float = (math.sqrt(3) * cohesion * math.cos(math.radians(internal_friction_angle))) / math.sqrt(3 + factor_a)
                # print("depth为{}，K为{}".format(depth, K))

                #  计算Pm  临界密度
                p_m: float = 1.0
                # 设置迭代的要求
                step_size: float = 0.1  # 迭代步长为 0.1
                # 定义停止条件
                tolerance = 1e-6  # 容差值
                max_iterations = 1000  # 最大迭代次数
                iteration = 0
                theta: float = optimal_theta
                # theta = 86.3
                # 求解三个带有p_m的公式
                while True:

                    # *******************************
                    # 计算sigma_r
                    sigma_r: float = p_m - osmotic_coefficient * void_ratio * (p_m - in_situ_pore_pressure)
                    # 计算sigma_theta
                    factor_a: float = (formation_stress_factor * (1 - 2 * poisson_ratio)) / (1 - poisson_ratio)
                    sigma_theta: float = sigma_xx + sigma_yy - 2 * (sigma_xx - sigma_yy) * math.cos(math.radians(2 * theta)) - \
                                  4 * tau_xy * math.sin(math.radians(2 * theta)) - p_m + \
                                  osmotic_coefficient * (p_m - in_situ_pore_pressure) * (factor_a - void_ratio)
                    # 计算sigma_z
                    sigma_z: float = sigma_zz - (2 * poisson_ratio * (sigma_xx - sigma_yy) * math.cos(math.radians(2 * theta)) -
                                          4 * poisson_ratio * tau_xy * math.sin(math.radians(2 * theta))) + \
                              osmotic_coefficient * (p_m - in_situ_pore_pressure) * (factor_a * 0.5 - void_ratio)
                    # *******************************
                    # 计算tau_theta_z
                    tau_theta_z: float = 2 * tau_yz * math.cos(math.radians(theta)) - 2 * tau_xz * math.sin(math.radians(theta))
                    # 计算tau_rz, tau_r_theta
                    tau_rz: float = 0
                    tau_r_theta: float = 0
                    # 计算sigma_one
                    sigma_one: float = sigma_r - formation_stress_factor * in_situ_pore_pressure
                    # 计算sigma_two
                    factor_b: float = (sigma_theta + sigma_z) / 2
                    factor_c: float = math.pow((sigma_theta - sigma_z), 2) + 4 * math.pow(tau_theta_z, 2)
                    factor_d: float = formation_stress_factor * in_situ_pore_pressure
                    sigma_two: float = (factor_b + math.sqrt(factor_c) / 2) - factor_d
                    # 计算sigma_three
                    sigma_three: float = (factor_b - math.sqrt(factor_c) / 2) - factor_d
                    # 计算J_two
                    factor_e: float = math.pow((sigma_one - sigma_two), 2)
                    factor_f: float = math.pow((sigma_two - sigma_three), 2)
                    factor_g: float = math.pow((sigma_three - sigma_one), 2)
                    J_two: float = (factor_e + factor_f + factor_g) / 6
                    # 计算I_one
                    I_one: float = sigma_one + sigma_two + sigma_three
                    y_result: float = math.sqrt(J_two) - R * I_one - K
                    # 更新 p_m 的值
                    p_m += step_size
                    # 检查停止条件
                    if y_result < tolerance:
                        p_m = p_m
                        break
                    iteration += 1
                # 临界钻井液密度
                rh0 = (p_m / depth) * 100
                result.rh0 = rh0
                # print("depth为{}，rh0为{}".format(depth, rh0))
                xb: float = drilling_fluid_density / rh0
                result.drilling_fluid_density = drilling_fluid_density
                result.xb = xb
                # print("depth为{}，xb为{}".format(depth, xb))
                N = ((3 * 2 * borehole_radius) / yang_modulus_elasticity) * (
                        (max_horizontal_ground_stress + min_horizontal_ground_stress) + 2 * (
                        max_horizontal_ground_stress - min_horizontal_ground_stress))
                # print("depth为{}，N为{}".format(depth, N))
                result.N = N

                if xb < 1:
                    is_Not_Safe = "危险"
                    result.is_Not_Safe = is_Not_Safe

                elif (xb >= 1) and (xb < 1.2):
                    is_Not_Safe = "基本安全"
                    result.is_Not_Safe = is_Not_Safe

                elif xb > 1.2:
                    is_Not_Safe = "安全"
                    result.is_Not_Safe = is_Not_Safe

                stability_result.append(result)
    stability_result_json = json.loads(JsonUtil.obj2str(stability_result))
    df = pd.DataFrame(stability_result_json)
    # 生成并上传Excel数据
    excel_data = df
    # 将数据写入Excel文件
    excel_file = io.BytesIO()
    excel_data.to_excel(excel_file, index=False)
    excel_file.seek(0)
    # putObject('well', data_object_name, excel_file)
    # 定义第一个为安全系数为危险的位置 is_safe 为true就是当前井壁安全 输出下边的参数为默认0
    is_safe = True
    first_dangerous_depth: float = 0.0
    first_dangerous_rho: float = 0.0
    first_dangerous_N: float = 0.0
    first_dangerous_density: float = 0.0
    # 找最大的变形量位置
    max_N: float = 0.0
    max_N_depth: float = 0.0
    for result_item in stability_result:
        if result_item.depth >= 200 and result_item.is_Not_Safe == '危险':
            is_safe = False
            first_dangerous_depth = result_item.depth
            first_dangerous_rho = result_item.rh0
            first_dangerous_N = result_item.N
            first_dangerous_density = result_item.drilling_fluid_density
        if result_item.N > max_N:
            max_N = result_item.N
            max_N_depth = result_item.depth
    # 找危险的位置 大于200米开始
    is_safe = is_safe
    first_dangerous_depth = first_dangerous_depth
    first_dangerous_rho = first_dangerous_rho
    first_dangerous_N = first_dangerous_N
    first_dangerous_density = first_dangerous_density
    # 找最大的变形量
    max_N = max_N
    max_N_depth = max_N_depth

    return {
        # 如果is_safe为False，就输出当前的危险井段的井深，扩径最大值，钻井液最小密度和实际的密度
        # ************************
        "is_safe": is_safe,
        "first_dangerous_depth": first_dangerous_depth,
        "first_dangerous_N": first_dangerous_N,
        "first_dangerous_rho": first_dangerous_rho,
        "first_dangerous_density": first_dangerous_density,
        # ************************
        "max_N": round(max_N, 4),
        "max_N_depth": max_N_depth
    }

